package zjitc.servlet;

import zjitc.domain.PageBean;
import zjitc.domain.User;
import zjitc.service.UserService;
import zjitc.service.UserServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.Map;

@WebServlet("/findUserByPageServlet")
public class FindUserByPageServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码
        request.setCharacterEncoding("utf-8");
        //1.获取参数
        String currentPage = request.getParameter("currentPage");//当前页码
        String rows = request.getParameter("rows"); //每页显示条数
        if(currentPage==null||"".equals(currentPage)){
            currentPage="1";
        }
        if(rows==null||"".equals(rows)){
            rows="5";
        }
        //2. 调用service查询
        Map<String, String[]> condition = request.getParameterMap();

        UserService service = new UserServiceImpl();
        PageBean<User> pb = service.findUserByPage(currentPage,rows,condition);

        //3. 将PageBean存入request
        request.setAttribute("pb",pb);
        System.out.println(pb.getList());
        //将获取到的condition集合存入request域
        request.setAttribute("condition",condition);
        //4. 转发到list.jsp
        request.getRequestDispatcher("/list.jsp").forward(request,response);

    }
}
